package weeny.demo;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Point2D;
import java.util.Random;

import javax.swing.JButton;

import prefuse.action.ActionList;
import prefuse.action.RepaintAction;
import prefuse.util.FontLib;

import weeny.pre.display.MarkerDisplay;
import weeny.pre.util.IntColor;
import weeny.pre.util.Marker;
import weeny.util.Counter;
import weeny.wordle.pre.WordleLabelLayout;

public class WordleDemo extends MarkerDisplay{
	private static Random RAND = new Random(12345L);

	private void useAIGWords(){
		Counter<String> counter = new Counter<String>();
		counter.increase("insurance",659);
		counter.increase("president",143);
		counter.increase("percent",301);
		counter.increase("subject",153);
		counter.increase("street",280);
		counter.increase("aig",423);
		counter.increase("people",209);
		counter.increase("investment",260);
		counter.increase("business",168);
		counter.increase("mccain",255);
		counter.increase("time",262);
		counter.increase("federal",104);
		counter.increase("money",245);
		counter.increase("pay",112);
		counter.increase("says",155);
		counter.increase("crisis",328);
		counter.increase("government",428);
		counter.increase("credit",275);
		counter.increase("obama",245);
		counter.increase("financial",1000);
		counter.increase("going",124);
		counter.increase("text",126);
		counter.increase("housing",233);
		counter.increase("debt",100);
		counter.increase("national",155);
		counter.increase("billion",360);
		counter.increase("mortgage",262);
		counter.increase("economy",245);
		counter.increase("companies",518);
		counter.increase("problem",129);
		counter.increase("economic",153);
		counter.increase("loans",172);
		counter.increase("assets",168);
		counter.increase("system",117);
		counter.increase("funds",117);
		counter.increase("market",688);
		counter.increase("plan",160);
		counter.increase("democratic",114);
		counter.increase("prices",131);
		counter.increase("years",272);
		counter.increase("securities",185);
		counter.increase("group",112);
		counter.increase("make",163);
		counter.increase("world",194);
		counter.increase("wall",258);
		counter.increase("bailout",250);
		counter.increase("week",197);
		counter.increase("banks",885);
		
		Font font = FontLib.getFont("Papyrus", 10);
		for(String key : counter.keys()){
			Point2D pos = new Point2D.Double(RAND.nextInt(getWidth()),
					RAND.nextInt(getHeight()));
			float size = counter.getCount(key).floatValue()/50;
			drawString(key, pos, IntColor.BROWN, size).setFont(font);
		}
	}
	
	@SuppressWarnings("unused")
	private void useRandomWords(){
		for(int i = 0; i<100; i++){
			String word = "";
			int lenth = 3+RAND.nextInt(5);
			for(int j = 0; j<lenth; j++){
				word += (char)('a'+RAND.nextInt(26));
			}
			Point2D pos = new Point2D.Double(RAND.nextInt(getWidth()),
					RAND.nextInt(getHeight()));
			drawString(word, pos, IntColor.BROWN, i!=0?RAND.nextInt(3)+1:30);
		}
	}
	
	private WordleDemo() {
		setSize(800,600);
		
		useAIGWords();
		
		showIODisplayTab();
		showItemControlTabInTree();
		
		//the layout applys to every item in TEXT_GROUP
		WordleLabelLayout layout = new WordleLabelLayout(Marker.TEXT_GROUP);
		ActionList action = new ActionList();
		action.add(layout);
		action.add(new RepaintAction());
		getVisualization().putAction("layout", action);
		
		JButton runButton = new JButton("(re-)run wordle");
		runButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getVisualization().run("layout");
			}
		});
		addWidget(IO_DISPLAY_TAB, runButton);
	}

	public static void main(String[] args) {
		new WordleDemo().showFrame("Wordle Demo");
	}
}